package performanceTest;

import org.junit.Test;

import java.util.stream.LongStream;

public class SumTest {

    private final long count = 1000_000_000_000l;

    /**
     * 2m 16s
     */
    @Test
    public void simple() {

        long sum = 0;
        for (long i = 0; i < count; i++) {
            sum += i;
        }
        System.out.println(sum);
    }

    /**
     * 5m 11s
     */
    @Test
    public void stream() {

        long sum = LongStream.range(0, count)
                .sequential() //设置为顺序流
                .sum();
        System.out.println(sum);
    }

    /**
     * 8m 14s
     */
    @Test
    public void parallelStream() {

        long sum = LongStream.range(0, count)
                .parallel() //设置为并行流
                .sum();
        System.out.println(sum);
    }
}
